package com.ruoyi.business.util;


import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;

import com.ruoyi.bizsys.utils.BizConstants;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ServletContextAware;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class InitDataConfig implements InitializingBean, ServletContextAware {

	public static Map<String, String> LOAN_ORDER_STATUS_MAP = new HashMap<String, String>();

    public static Map<String, String> LOAN_IOU_STATUS_MAP = new HashMap<String, String>();
    
    public static final DecimalFormat currency = new DecimalFormat("#0.00");

    @Autowired
	ISysDictDataService sysDictDataService;
    
    private Map<String, String> dictListToMap(List<SysDictData> sysDictDataList){
    	Map<String, String> map = new HashMap<String, String>();
    	for(SysDictData o:sysDictDataList) {
    		map.put(o.getDictValue(), o.getDictLabel());
    	}
    	return map;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
    	SysDictData dictData = new SysDictData();
    	
    	dictData.setDictType(BizConstants.LOAN_ORDER_DICT_TYPE);
    	List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(dictData);
    	this.LOAN_ORDER_STATUS_MAP = this.dictListToMap(sysDictDataList);
    	
    	dictData.setDictType(BizConstants.LOAN_IOU_DICT_TYPE);
    	sysDictDataList = sysDictDataService.selectDictDataList(dictData);
    	this.LOAN_IOU_STATUS_MAP = this.dictListToMap(sysDictDataList);
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
    }
}